package com.yonyougov.devops.cruxcisupport.harbor.artifact;

import com.yonyougov.devops.cruxcisupport.harbor.artifact.Dto.DelHarborArtifactDto;
import com.yonyougov.devops.cruxcisupport.harbor.artifact.Dto.ReferencyAndVersion;
import com.yonyougov.devops.cruxcisupport.harbor.repositories.HarborRepositories;
import com.yonyougov.devops.cruxcisupport.harbor.repositories.HarborRepositoriesDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HarborArtifactServiceImpl implements HarborArtifactService {
    @Autowired
    private HarborArtifactDao harborArtifactDao;
    @Autowired
    private HarborRepositoriesDao harborRepositoriesDao;


    @Override
    public List<HarborArtifacts> getArtifacts(String projectName, String repositoryName,String tag,Integer page, Integer pageSize) {
        return harborArtifactDao.getArtifacts(projectName,repositoryName,tag,page,pageSize);
    }

    @Override
    public List<HarborArtifacts> getAllArtifact(String projectName, String repositoryName, String tag , Integer page, Integer pageSize, String order, String sort, String startTime, String outTime) {
        List<HarborArtifacts> allArtifact = harborArtifactDao.getArtifacts(projectName, repositoryName, tag, 1, 999999);
        //筛选
        if(startTime != null && outTime != null ){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date sTime = null;
            Date oTime = null;
            try {
                sTime= simpleDateFormat.parse(startTime);
                oTime= simpleDateFormat.parse(outTime);
                Date finalSTime = sTime;
                Date finalOTime = oTime;
                allArtifact = allArtifact.stream().filter(h2 ->( h2.getPush_time().after(finalSTime) && h2.getPush_time().before(finalOTime) )).collect(Collectors.toList());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //排序
        if(sort==null){
            sort="1";
        }
        if(sort.equals("pushTime")){
            if(order.equals("descend")){
                Collections.sort(allArtifact, new Comparator<HarborArtifacts>() {
                    @Override
                    public int compare(HarborArtifacts h1, HarborArtifacts h2) {
                        return -h1.getPush_time().compareTo(h2.getPush_time());
                    }
                });
            }else
                Collections.sort(allArtifact, new Comparator<HarborArtifacts>() {
                    @Override
                    public int compare(HarborArtifacts h1, HarborArtifacts h2) {
                        return h1.getPush_time().compareTo(h2.getPush_time());
                    }
                });
        }else if (sort.equals("pullTime")){
            if(order.equals("descend")){
                Collections.sort(allArtifact, new Comparator<HarborArtifacts>() {
                    @Override
                    public int compare(HarborArtifacts h1, HarborArtifacts h2) {
                        return -h1.getPull_time().compareTo(h2.getPull_time());
                    }
                });
            }else
                Collections.sort(allArtifact, new Comparator<HarborArtifacts>() {
                    @Override
                    public int compare(HarborArtifacts h1, HarborArtifacts h2) {
                        return h1.getPull_time().compareTo(h2.getPull_time());
                    }
                });
        }

  //   if (page == 1) {
  //       if ((page * pageSize) <= allArtifact.size()) {
  //           return allArtifact.subList(0, (page * pageSize));
  //       } else {
  //           return allArtifact.subList(0, allArtifact.size());
  //       }
  //   } else {
  //       if ((page * pageSize) <= allArtifact.size()) {
  //           return allArtifact.subList(((page - 1) * pageSize) + 1, (page * pageSize));
  //       } else {
  //           return allArtifact.subList(((page - 1) * pageSize) + 1, allArtifact.size());
  //       }
  //   }
        return allArtifact;
    }
    @Override
    public void deleteArtifact(String projectName, String repositoryName, ReferencyAndVersion referencyAndVersion) {
        harborArtifactDao.deleteArtifact(projectName,repositoryName,referencyAndVersion);
    }

    @Override
    public Integer getArtifactRepositoryTotal(String projectName, String repository) {
        List<HarborRepositories> harborRepositoriesList = harborRepositoriesDao.getRepositoriesByName(projectName, repository, 1, 10);
        if(CollectionUtils.isNotEmpty(harborRepositoriesList)){
            return harborRepositoriesList.get(0).getArtifact_count();
        }
        return null;
    }

    @Override
    public void batchDelArtifacts(List<DelHarborArtifactDto> delHarborArtifactDtoList) {
        for (DelHarborArtifactDto delHarborArtifactDto: delHarborArtifactDtoList) {
            String project = delHarborArtifactDto.getProject();
            String repository = delHarborArtifactDto.getRepository();
            ReferencyAndVersion referencyAndVersion = delHarborArtifactDto.getReferencyAndVersion();
            if(StringUtils.isNotBlank(referencyAndVersion.getReference())){
                log.debug("开始删除Harbor中项目:{}下仓库:{}下的镜像的dig:{}",project,repository,referencyAndVersion.getReference());
                deleteArtifact(project,repository,referencyAndVersion);
                log.debug("删除Harbor中项目:{}下仓库:{}下的镜像的dig:{} 成功",project,repository,referencyAndVersion.getReference());
            }
        }
    }
    public boolean findAppVersionfromHaber(String appName,String versionName){
        try {
            harborArtifactDao.getArtifactByAppAndVersion("crux",appName,versionName);
            return true;
        }catch (NullPointerException e)
        {
            return  false;
        }
    };
}
